class Char
  @@resource ||= Hash.new
  @@var ||= Hash.new
  
  def self.add_resources(*resource_names)
    resource_names.each do |name|
      next if @@resource.key?(name) || @@var.key?(name)
      @@resource[name] = {'current' => 0, 'max' => 1}
      (class << self; self; end).module_eval do
        define_method(name) {@@resource[name]['current']}
        define_method("max_#{name}") {@@resource[name]['max']}
        define_method("#{name}=") {|value| @@resource[name]['current'] = value.to_i}
        define_method("max_#{name}=") {|value| @@resource[name]['max'] = value.to_i}
        define_method("percent_#{name}") {((@@resource[name]['current']*100.0/@@resource[name]['max'])*1000).to_i/1000.0}
      end
    end
  end
  def self.add_vars(*var_names)
    var_names.each do |name|
      next if @@resource.key?(name) || @@var.key?(name)
      @@var[name] = nil
      (class << self; self; end).module_eval do
        define_method(name) {@@var[name]}
        define_method("set_#{name}") {|value| @@var[name] = value}
        define_method("#{name}=") {|value| @@var[name] = value}
      end
    end
  end
  def self.send_set(send, var, set)
    return unless @@var.key?(var)
    put(send)
    @@var[var] = set
  end
  def self.[](k)
    return @@var[k] if @@var.has_key?(k)
    if k =~ /max(.+)/
      return @@resource[$1]['max'] if @@resource.key?($1)
    else
      return @@resource[k]['current'] if @@resource.key?(k)
    end
    puts "CHAR.#{k} NOT ADDED."
    nil
  end
  def self.[]=(k, v)
    (@@var[k] = v;return) if @@var.has_key?(k)
    if k =~ /max(.+)/
      (@@resource[$1]['max'] = v;return) if @@resource.key?($1)
    else
      (@@resource[k]['current'] = v;return) if @@resource.key?(k)
    end
    puts "CHAR.#{k} NOT ADDED."
    nil
  end
  def self.resources; @@resource.dup end
  def self.vars; @@var.dup end
end